<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
  </head>
  <body>
    <div id="root"></div>

    <script type="text/babel">
      class Clock extends React.Component {
        state = {
          data: new Date(),
        };

        tick() {
          this.setState({
            data: new Date(),
          });
        }

        componentDidMount() {
          // 当DOM成功挂载以后就会执行该函数
          const timerId = setInterval(() => {
            this.tick();
          }, 1000);
        }

        componentWillUnmount() {
          // 当DOM从页面中去除的时候清除计时器
          clearInterval(this.timerId);
        }

        render() {
          return (
            <div>
              <h2>WelCome</h2>
              <p>{this.state.data.toLocaleTimeString()}</p>
            </div>
          );
        }
      }

      ReactDOM.render(<Clock />, document.getElementById("root"));
    </script>
  </body>
</html>
